package com.ecobell.camera;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.ecobell.R;
import com.ecobell.util.Image;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

public class GeneralCamera extends Activity {
	private static final String TAG = GeneralCamera.class.getSimpleName();
	
	private Preview mPreview;
    private Button mSnapshot;
	
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN );
		setContentView(R.layout.general_camera);

        
		mSnapshot = (Button) findViewById(R.id.buttonSnapshot);
		mSnapshot.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPreview.mCamera.takePicture(shutter, raw, jpeg);
			}
			
		});
		
		mPreview = new Preview(this);
		FrameLayout fl = (FrameLayout) findViewById(R.id.previewStage);
		fl.addView(mPreview);
		
		mPreview.invalidate();
	}
	
	
	
	private ShutterCallback shutter = new ShutterCallback() {

		@Override
		public void onShutter() {
			Log.d(TAG, "onShutter");
		}
    	
    };
    
    private PictureCallback raw = new PictureCallback() {

		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			Log.d(TAG, "onPictureTaken - raw");
		}
    	
    };
    
    private PictureCallback jpeg = new PictureCallback() {

    	
    	
		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			Log.i(TAG, "onPictureTaken - start");
			
			
			File path = Environment.getExternalStoragePublicDirectory(
	                Environment.DIRECTORY_PICTURES);
			File file = new File(path, String.format("%d-%d-%d.jpg", System.currentTimeMillis(), 0, 0));
			try {
				// Make sure the Pictures directory exists.
	            path.mkdirs();
	            
	            ByteArrayOutputStream buf = new ByteArrayOutputStream();
	            
	            Log.i(TAG, "onPictureTaken - converting YCrCb to RGB");
				Image conv = new Image();
				int pictureWidth = mPreview.getWidth();
				int pictureHeight = mPreview.getHeight();
				int[] rgbBuffer = new int[pictureWidth * pictureHeight];
				conv.ConvertYCbCrToRGB(data, data.length, rgbBuffer, pictureWidth, pictureHeight);
//				
//				Bitmap img = Bitmap.createBitmap(rgbBuffer, pictureWidth, pictureHeight, Bitmap.Config.ARGB_8888);
//				
//				img.compress(CompressFormat.JPEG, 100, buf);
//				img.recycle();
//				img = null;
				Log.i(TAG, "onPictureTaken - done converting");
	            
				buf.flush();
				
				FileOutputStream fos = new FileOutputStream(file);
				fos.write(buf.toByteArray());
				
				fos.flush();
				Log.i(TAG, "onPictureTaken - wrote byte: " + data.length);
				
				// Tell the media scanner about the new file so that it is
	            // immediately available to the user.
	            MediaScannerConnection.scanFile(
	            		GeneralCamera.this,
	            		new String[] {file.toString() },
	            		null, 
	            		new MediaScannerConnection.OnScanCompletedListener() {
							
							@Override
							public void onScanCompleted(String path, Uri uri) {
								Log.i(TAG, "Scanned " + path + ":");
			                    Log.i(TAG, "-> uri=" + uri);
							}
						}
	            		);
	            
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			Log.d(TAG, "onPictureTaken - jpeg has been called");
		}
    	
    };

}
